#include <QtGui>

#include "objectDialog.h"
#include "src/vector3d.h"
#include "defines/treeType.h"
#include "faceTreeItem.h"
#include "edgeTreeItem.h"
#include "vertexTreeItem.h"

ObjectDialog::ObjectDialog(int _id, QWidget *_parent)
    : QDialog(_parent), id(_id){

    object = NULL;
    colorR = colorG = colorB = colorAlpha = 255;
    colorChanged = false;
}

ObjectDialog::ObjectDialog(TreeItem *_item, QWidget *_parent)
    : QDialog(_parent), item(_item){

    setWindowTitle(QString::fromUtf8("Modificar a cor "));

    switch(_item->getType())
    {
        case OBJECT:
        {
            object = ((ObjectTreeItem *)item)->getObject();
            break;
        }
        case DEFAULT:
        {
            QTreeWidgetItem *item2 = ((QTreeWidgetItem *)item)->parent();
            object = ((ObjectTreeItem *)item2)->getObject();
            break;
        }
        default:
        {
            QTreeWidgetItem *item2 = ((QTreeWidgetItem *)item)->parent();
            item2 = item2->parent();
            object = ((ObjectTreeItem *)item2)->getObject();
            break;
        }
    }

    colorR = colorG = colorB = colorAlpha = 255;

    QVBoxLayout *mainLayout = createColorButtonLayout();
    colorChanged = false;

    setLayout(mainLayout);
}

ObjectDialog::~ObjectDialog(){
    delete colorPushButton;
}

QVBoxLayout *ObjectDialog::createColorButtonLayout(){
    colorPushButton = new QPushButton(tr("Cor"));
//    colorPushButton->setFlat(true);
    connect(colorPushButton, SIGNAL(clicked()), this, SLOT(changeColor()));
    setButtonColor();

    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(acceptAndCreate()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    buttonBox->setLocale(QLocale(QLocale::Portuguese, QLocale::Brazil));

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(colorPushButton);
    mainLayout->addWidget(buttonBox);
    mainLayout->addStretch(1);

    return mainLayout;
}

void ObjectDialog::setButtonColor(){
    //    QString colorStr = QString::fromUtf8("color: rgb(255, 255, 255);\n" "background-color: rgb(85, 0, 0, 255);");
    QString colorStr;
    if(colorR + colorB + colorG + colorAlpha < 600){
        colorStr = QString::fromUtf8("color: rgb(255, 255, 255);\n");
    }else{
        colorStr = QString::fromUtf8("color: rgb(0, 0, 0);\n");
    }
    colorStr.append(QString::fromUtf8("background-color: rgb("));
    colorStr.append(QString::number(colorR));
    colorStr.append(QString::fromUtf8(", "));
    colorStr.append(QString::number(colorG));
    colorStr.append(QString::fromUtf8(", "));
    colorStr.append(QString::number(colorB));
    colorStr.append(QString::fromUtf8(", "));
    colorStr.append(QString::number(colorAlpha));
    colorStr.append(QString::fromUtf8(");"));

    colorPushButton->setStyleSheet(colorStr);
}

void ObjectDialog::acceptAndCreate(){
    if(colorChanged)
    {

        switch(item->getType())
        {
            case FACE:
            {
                ((FaceTreeItem *)item)->getFace()->setColor(Color(colorR / 255.0, colorG / 255.0, colorB / 255.0, colorAlpha / 255.0));
                break;
            }
            case EDGE:
            {
                ((EdgeTreeItem *)item)->getEdge()->setColor(Color(colorR / 255.0, colorG / 255.0, colorB / 255.0, colorAlpha / 255.0));
                break;
            }
            case VERTEX:
            {
                ((VertexTreeItem *)item)->getVertex()->setColor(Color(colorR / 255.0, colorG / 255.0, colorB / 255.0, colorAlpha / 255.0));
                break;
            }
            default:
            {
                object->setColor(Color(colorR / 255.0, colorG / 255.0, colorB / 255.0, colorAlpha / 255.0));
                break;
            }
        }
    }

    emit done();
    accept();
}

void ObjectDialog::changeColor(){
    QColor color = QColorDialog::getColor(QColor(colorR, colorG, colorB, colorAlpha), this, "Escolha a cor", QColorDialog::ShowAlphaChannel);
    if (color.isValid()){
        colorR = color.red();
        colorG = color.green();
        colorB = color.blue();
        colorAlpha = color.alpha();
    }else return;
    setButtonColor();

    colorChanged = true;
}
